<?php
/**
 * Created by PhpStorm.
 * User: 热心观众
 * Date: 2018/10/7
 * Time: 14:15
 */

namespace app\index\controller;
use think\Controller;
use think\Db;
use app\common\sendSms\REST;
use think\Session;

class Register extends Controller
{
    public function Register()
    {
        return view('index/register');
    }

    public function DoRegister()
    {

        $data = input('post.');
//        dump($data);die;
        $data['zz_time']=date('Y-m-d');
//        dump($data);die;
        $code = Session::get('code');
//        dump($code);die;
        if ($data['code'] != $code){
            $this->error('验证码错误');
            exit;
        };
        unset($data['code']);
        if ($data['zz_pass']!= $data['repass']){
            $this->error('两次密码不一致');
            exit;
        }
        unset($data['repass']);
//        dump($data);die;
        $n = Db::table('zz_user')->where('zz_name',$data['zz_name'])->find();
        if ($n){
            $this->error('该用户名已被注册');
            exit;
        }
        $t = Db::table('zz_user')->where('zz_tel',$data['zz_tel'])->find();
        if ($t){
            $this->error('该手机号已被注册');
            exit;
        }
        $res = Db::table('zz_user')->insert($data);
        if ($res){
            $re = [];
            $re['status'] = 1;
            return json($re);
        }else{
            $re = [];
            $re['status'] = 0;
            return json($re);
        }
    }

    public function sendSms()
    {
//        $data = request()->get();
        $data['tel'] = input('zz_tel');
//        dump($data);die;
        $tel = $data['tel'];
//        dump($tel);die;
        if (!$tel){
            return returnJson('手机号不合法',0);
        }
        $code = mt_rand('999','9999');

        Session::set('code',$code);

        $this->sendTemplateSMS($tel,array($code,'10'),1);


    }
    public function sendTemplateSMS($to,$datas,$tempId)
    {
        //主帐号,对应开官网发者主账号下的 ACCOUNT SID
        $accountSid= '8a216da86619680501661ec3398d0329';

        //主帐号令牌,对应官网开发者主账号下的 AUTH TOKEN
        $accountToken= 'c8e8408e88d047f1ae991d9e704fe450';

        //应用Id，在官网应用列表中点击应用，对应应用详情中的APP ID
        //在开发调试的时候，可以使用官网自动为您分配的测试Demo的APP ID
        $appId='8a216da86619680501661ec339df032f';
        // 初始化REST SDK
//        global $accountSid,$accountToken,$appId;
        $rest = new REST();
        $rest->setAccount($accountSid,$accountToken);
        $rest->setAppId($appId);

        // 发送模板短信
        echo "Sending TemplateSMS to $to <br/>";
        $result = $rest->sendTemplateSMS($to,$datas,$tempId);
        if($result == NULL ) {
            echo "result error!";
        }
        if($result->statusCode!=0) {
            echo "error code :" . $result->statusCode . "<br>";
            echo "error msg :" . $result->statusMsg . "<br>";
            //TODO 添加错误处理逻辑
        }else{

            echo "Sendind TemplateSMS success!<br/>";
            // 获取返回信息
            $smsmessage = $result->TemplateSMS;
            echo "dateCreated:".$smsmessage->dateCreated."<br/>";
            echo "smsMessageSid:".$smsmessage->smsMessageSid."<br/>";

            //TODO 添加成功处理逻辑
        }

    }
}